package com.csc531.edit;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.csc531.R;
import com.csc531.adapters.ManagerDbAdapter;


public class NotesEdit extends Activity 
{
	private static final String TAG = "notesedit";

	static ArrayList<String> _coursesList;

	//menu
	static final int SAVE = Menu.FIRST;

	//View fields
	private EditText _noteTitleText;
	private EditText _noteBodyText;

	//database 
	private Long _rowId;
	private ManagerDbAdapter _mDbHelper;

	static final int TIME_DIALOG_ID = 0;
	static final int DATE_DIALOG_ID = 1;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		super.onCreateOptionsMenu(menu);
		menu.add(0, SAVE, 0, R.string.task_edit_menu_item1);

		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		switch(item.getItemId()) 
		{
		case SAVE:
			saveState();
			exitActivity();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private void exitActivity()
	{
		setResult(RESULT_OK);
		saveState();
		finish();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		_mDbHelper = new ManagerDbAdapter(this);
		_mDbHelper.open();
		setContentView(R.layout.note_edit);

		_noteTitleText = (EditText) findViewById(R.id.title);
		_noteBodyText = (EditText) findViewById(R.id.body);

		dealWithNoteData(savedInstanceState);
	}

	private void dealWithNoteData(Bundle savedInstanceState) 
	{
		_rowId = savedInstanceState != null ? savedInstanceState.getLong(ManagerDbAdapter.KEY_ROWID) : null;

		if(_rowId == null) 
		{
			Bundle extras = getIntent().getExtras();
			_rowId = extras != null ? extras.getLong(ManagerDbAdapter.KEY_ROWID) : null;
		}

		populateFields();
	}

	private void populateFields() 
	{
		if (_rowId != null) 
		{
			Cursor note = _mDbHelper.fetchNote(_rowId);
			startManagingCursor(note);
			note.moveToFirst();

			_noteTitleText.setText(note.getString(
					note.getColumnIndexOrThrow(ManagerDbAdapter.KEY_NOTE_TITLE)));
			_noteBodyText.setText(note.getString(
					note.getColumnIndexOrThrow(ManagerDbAdapter.KEY_NOTE_BODY)));
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) 
	{
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(_noteTitleText.getWindowToken(), 0);
		
		return true;
	}
	
	@Override
	protected void onPause() 
	{
		super.onPause();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		populateFields();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) 
	{
		super.onSaveInstanceState(outState);
		outState.putLong(ManagerDbAdapter.KEY_ROWID, _rowId);
	}

	private void saveState() 
	{
		if( isValidView() == false ) 
		{
			return;
		}
		if (_rowId == null)  
		{
			addNoteToDatabase();
		} 
		else 
		{
			updateNoteInDatabase();
		}
	}

	private boolean isValidView()
	{
		String title = _noteTitleText.getText().toString();

		if( title.length() == 0 ) 
		{
			Log.v(TAG,"Nothing in here returning nothing");
			return false;
		}	
		else
		{
			return true;
		}
	}

	private void addNoteToDatabase()
	{
		String title = _noteTitleText.getText().toString();
		String body = _noteBodyText.getText().toString();

		Log.v(TAG,"SAVING note: "+title);
		long id = _mDbHelper.createNote(title, body);
		if (id > 0) 
		{
			_rowId = id;
		}
	}

	private void updateNoteInDatabase()
	{
		String title = _noteTitleText.getText().toString();
		String body = _noteBodyText.getText().toString();

		Log.v(TAG,"UPDATING note: "+title);
		_mDbHelper.updateNote(_rowId, title, body);
	}
}
